package com.srd.phone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.View;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.bean.BankList;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.wheel.widget.SingleDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 银行卡管理
 */
public class SafeBankCardManageActivity extends BaseActivity implements View.OnClickListener {

    private UtilPresenter utilPresenter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_bank_card_manage);

        initView();
        initData();
    }


    @Override
    public void initView() {
        super.initView();
        tv_common_title.setText("银行卡管理");
        findViewById(R.id.tv_bank_card_bound).setOnClickListener(this);
        findViewById(R.id.tv_bank_card_unbound).setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();

        showWaitingDialog();
        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter) presenter;
        setUrlArrs(Constants.YBANK_LIST, Constants.BANK_LIST);
        getData();
    }

    private void getData() {
        utilPresenter.noParams(Constants.YBANK_LIST);
    }

    private String url = "";

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        cancelWaitingDialog();
        if (Constants.YBANK_LIST.equals(tag)) {
            try {
                JSONObject jsonObj = new JSONObject(data);
                url = jsonObj.optString("data");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(Constants.BANK_LIST.equals(tag)){
            List<BankList> list = BankList.parseData(data);
            String[] array = new String[list.size()];
            for (int i = 0; list != null && i < list.size(); i++){
                BankList item = list.get(i);
                array[i] = item.banknm + "(尾号" + item.numbers + ")";
            }

            SingleDialog.makeDialog(this, array, new SingleDialog.OnAddressCListener() {
                @Override
                public void onClick(String itemName) {
                    Snackbar.make(tv_common_title, itemName, Snackbar.LENGTH_LONG).show();
                }
            }).show();
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        cancelWaitingDialog();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_bank_card_bound:

                if(!TextUtils.isEmpty(url)){
                    Intent intent = new Intent(SafeBankCardManageActivity.this, WebViewActivity.class);
                    intent.putExtra("urlValue", url);
                    intent.putExtra("titleName", "银行卡绑定");
                    gotoActivity(intent);
                }else{
                    toastShow("url地址不正确~");
                }

                break;
            case R.id.tv_bank_card_unbound:

                utilPresenter.noParams(Constants.BANK_LIST);

                break;
        }
    }
}
